package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.apis.article.ArticleFeignClient;
import com.heima.common.constants.BehaviorConstants;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.UserRelationDto;
import com.heima.model.user.pojo.ApUser;
import com.heima.model.user.pojo.ApUserFollow;
import com.heima.user.interceotor.AppTokenInterceptor;
import com.heima.user.mapper.ApUserFollowMapper;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.UserFollowService;
import com.heima.utils.common.AppThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class UserFollowServiceImpl implements UserFollowService {
    @Autowired
    private ApUserFollowMapper apUserFollowMapper;
    @Autowired
    private ApUserMapper apUserMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private ArticleFeignClient articleFeignClient;



    /**
     * 用户关注作者
     * @param dto
     * @return
     */
    @Override
    public ResponseResult userFollow(UserRelationDto dto) {
        //1.校验参数
        if (dto==null || dto.getArticleId()==null || dto.getAuthorId()==null || dto.getOperation()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //2.获取用户id
        Integer userId = AppThreadLocalUtil.getUser().getId();
        if (userId == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        //3.判断用户的操作类型
        if (dto.getOperation()==0){
            //关注：新增数据
            ApUserFollow apUserFollow = new ApUserFollow();
            //根据作者id查询作者名称
            ResponseResult<ApArticle> article = articleFeignClient.findArticleById(dto.getAuthorId());
            apUserFollow.setFollowId(dto.getAuthorId().longValue());
            apUserFollow.setUserId(userId.longValue());
            apUserFollow.setFollowName(article.getData().getAuthorName());
            apUserFollow.setCreatedTime(new Date());
            apUserFollow.setIsNotice((short)1);
            apUserFollow.setLevel((short)1);
            apUserFollowMapper.insert(apUserFollow);

            //存储到redis中
            redisTemplate.opsForHash().put(BehaviorConstants.BEHAVIOR_FOLLOW+dto.getAuthorId(),
                    BehaviorConstants.BEHAVIOR_IS_FOLLOW+userId,
                    "1");
        }
        if (dto.getOperation()==1){
            //取消关注,删除数据库
            LambdaQueryWrapper<ApUserFollow> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ApUserFollow::getFollowId,dto.getArticleId())
                    .eq(ApUserFollow::getUserId,userId);
            apUserFollowMapper.delete(queryWrapper);

            //删除redis中的缓存
            redisTemplate.opsForHash().delete(BehaviorConstants.BEHAVIOR_FOLLOW+dto.getAuthorId(),
                    BehaviorConstants.BEHAVIOR_IS_FOLLOW+userId);
        }

        return ResponseResult.okResult("");
    }
}
